using System;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This is a test class for Pegasus.Runtime.Serialization.Formatters.Xml.XmlFormatter2
	/// and will check the usage of surrogate object to serialize and deserialzie.
	///</summary>
	[TestClass]
	public class XmlFormatter2SurrogateTests
	{
		// Local Instance Values
		private TestContext m_testContextInstance;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:XmlFormatter2SurrogateTests"/> class.
		/// </summary>
		public XmlFormatter2SurrogateTests()
		{
		}

		/// <summary>
		/// Gets or sets the test context which provides information about and 
		/// functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return m_testContextInstance;
			}
			set
			{
				m_testContextInstance = value;
			}
		}

		/// <summary>
		/// Test the Serialize method using a surrogate.
		/// </summary>
		[TestMethod]
		public void SerializeObjectSurrogateSelectorTest()
		{
			// Build the object to serialize 
			NonserializationChild startObj = new NonserializationChild();
			startObj.Name = XmlFormatter2Helper.StringTestValue;
			startObj.Description = XmlFormatter2Helper.StringTestValue;
			startObj.Count = XmlFormatter2Helper.IntTestValue;

			// Build the surrogate objects
			StreamingContext streamingContext = new StreamingContext( StreamingContextStates.Persistence );
			SurrogateSelector surrogateSelector = new SurrogateSelector();
			surrogateSelector.AddSurrogate( typeof( NonserializationChild ), streamingContext, new NonChildSerializationSurrogate() );

			// Deserialize the buffer back to an object
			NonserializationChild finishedObj = (NonserializationChild) XmlFormatter2Helper.SerializeDeserialize( startObj, surrogateSelector, streamingContext );

			// Check test results
			Assert.IsNotNull( finishedObj, "Deserialize return null object" );
			Assert.AreEqual( finishedObj.Name, XmlFormatter2Helper.StringTestValue, "Name value is not right" );
			Assert.AreEqual( finishedObj.Description, XmlFormatter2Helper.StringTestValue, "Description value is not right" );
			Assert.AreEqual( finishedObj.Count, XmlFormatter2Helper.IntTestValue, "Count value is not right" );
		}

		/// <summary>
		/// Test the Serialize method using a surrogate.
		/// </summary>
		[TestMethod]
		public void SerializeStructSurrogateSelectorTest()
		{
			// Build the object to serialize 
			NonserializationStruct startObj = new NonserializationStruct();
			startObj.Name = XmlFormatter2Helper.StringTestValue;
			startObj.Description = XmlFormatter2Helper.StringTestValue;
			startObj.Count = XmlFormatter2Helper.IntTestValue;

			// Build the surrogate objects
			StreamingContext streamingContext = new StreamingContext( StreamingContextStates.Persistence );
			SurrogateSelector surrogateSelector = new SurrogateSelector();
			surrogateSelector.AddSurrogate( typeof( NonserializationStruct ), streamingContext, new StructSerializationSurrogate() );

			// Deserialize the buffer back to an object
			NonserializationStruct finishedObj = (NonserializationStruct) XmlFormatter2Helper.SerializeDeserialize( startObj, surrogateSelector, streamingContext );

			// Check test results
			Assert.AreEqual( finishedObj.Name, XmlFormatter2Helper.StringTestValue, "Name value is not right" );
			Assert.AreEqual( finishedObj.Description, XmlFormatter2Helper.StringTestValue, "Description value is not right" );
			Assert.AreEqual( finishedObj.Count, XmlFormatter2Helper.IntTestValue, "Count value is not right" );
		}
	}
}
